package cn.darkhorse.ticket.store.service.verification.impl;

import cn.darkhorse.exception.ServiceException;
import cn.darkhorse.ticket.store.dto.TicketProductDetailsDTO;
import cn.darkhorse.ticket.store.pojo.TicketInventory;
import cn.darkhorse.ticket.store.pojo.TicketInventorySurplus;
import cn.darkhorse.ticket.store.service.verification.SurplusCheckDTO;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @description: 入库数量校验
 * @author: mr.wang
 * @time: 2024/1/18 11:14
 */
@Data
@Component
public class BasePurchaseSurplus extends BaseRules{
	
	private List<TicketProductDetailsDTO> productDetailsDTOList;
	
	
	
	
	@Override
	protected void doCheck() {
		if(null == productDetailsDTOList){
			return;
		}
		Map<String, TicketProductDetailsDTO> productMap = surplusCheckDTOMap(productDetailsDTOList);
		Map<String, TicketInventorySurplus> surplusMap = getSurplusMap();
		Map<String, TicketInventory> inventoryMap = getInventoryMap();
		Set<Map.Entry<String, TicketProductDetailsDTO>> entries = productMap.entrySet();
		for(Map.Entry<String, TicketProductDetailsDTO> entry:entries){
			TicketProductDetailsDTO dto = entry.getValue();
			TicketInventorySurplus surplus = surplusMap.get(entry.getKey());
			TicketInventory inventory = inventoryMap.get(entry.getKey());
			if(null != inventory){
			
				if(surplus.getSurplus().add(dto.getQuantity()).compareTo(BigDecimal.ZERO) < 0){
					throw new ServiceException(String.format("【%s】修改或删除后的数量不足！，无法操作",
							dto.getWarehouseInfoName()+"-"+dto.getProductName()));
				}
				if(inventory.getQuantity().compareTo(surplus.getSurplus()) != 0){
					throw new ServiceException(String.format("【%s】库存数量与记录数量不相等，请联系管理员！",
							dto.getWarehouseInfoName()+"-"+dto.getProductName()));
				}
			}
		}
	}
	
	
	
	
	
}
